package com.demo.connectdbtest.module.xml;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * test success
 */
public class ParseXmlData {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        // 创建解析器工厂
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = factory.newDocumentBuilder();
        // 创建一个Document对象
        //Document doc = db.parse("D:\\workData\\javaProject\\connectDB\\src\\main\\java\\com\\demo\\connectdbtest\\module\\xml\\tsc_src_2024_07_15_08_40_16.xml");
        Document doc = db.parse("D:\\workData\\javaProject\\connectDB\\src\\main\\java\\com\\demo\\connectdbtest\\module\\xml\\caddconent0907.xml");
        NodeList bookList = doc.getElementsByTagName("error");
        // 获取节点个数
        System.out.println("一共有" + bookList.getLength() + "本书");
        // 遍历每个book节点
        for (int i = 0; i < bookList.getLength(); i++) {
            // 索引从零开始
            NamedNodeMap attributes = bookList.item(i).getAttributes();
            Node severity = attributes.getNamedItem("severity");
            System.out.println(severity.getTextContent());

//            for (int j = 0; j < attributes.getLength(); j++) {
//                System.out.println(severity.getTextContent());
//            }

            System.out.println("================分割线===================");
        }

    }
}
